package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DApplyforLoansMapper;
import com.weipai.demo.mapper.base.ApplyforLoansMapper;
import com.weipai.demo.model.ApplyforLoans;
import com.weipai.demo.service.ApplyforLoansService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import com.weipai.demo.util.VerificationCodeImgUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/28 17:39
 * @Version 1.0
 */
@Service
public class ApplyforLoansServiceImpl implements ApplyforLoansService {

    private static final Logger log = LoggerFactory.getLogger(CarPriceServiceImpl.class);

    @Autowired
    private ApplyforLoansMapper applyforLoansMapper;
    @Autowired
    private DApplyforLoansMapper dApplyforLoansMapper;


    //页面用户申请贷款
    @Override
    public Response applyFor(ApplyforLoans applyforLoans) {
        Response response=new Response();
        try {
            applyforLoans.setStatus(0);
            applyforLoans.setReveal(1);
            int insert = applyforLoansMapper.insert(applyforLoans);
            response.setCode(RetCode.APPLYFORSUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("applyFor"+e.getMessage());
        }
        return response;
    }

    //分页展示贷款信息
    @Override
    public LayuiTablePagingResult showApplyFor(Integer page,Integer size) {
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
          /*  Example example = new Example(ApplyforLoans.class);
            example.createCriteria().andEqualTo("status", applyforLoans.getStatus());*/
            //根据状态查询申请的用户
            List<ApplyforLoans> banners = applyforLoansMapper.selectAll();

            PageInfo<ApplyforLoans> pageInfo=new PageInfo<>(banners);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("showApplyFor"+e.getMessage());
        }
        return result;
    }


    //申请用户是否通过审核
    @Override
    public Response audit(Integer id, Integer status) {
        Response response=new Response();
        try {
            ApplyforLoans applyforLoans=new ApplyforLoans();
            applyforLoans.setStatus(status);
            applyforLoans.setId(id);
            if (id!=null){
                //int i = applyforLoansMapper.updateByPrimaryKey(applyforLoans);
                dApplyforLoansMapper.updateByid(id,status);
                response.setCode(RetCode.UPDATESUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("audit"+e.getMessage());
        }
        return response;
    }

    //申请贷款用户删除
    @Override
    public Response delete(Integer id) {
        Response response=new Response();
        if (id!=null){
            ApplyforLoans applyforLoans=new ApplyforLoans();
            applyforLoans.setId(id);
            int delete = applyforLoansMapper.delete(applyforLoans);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //图片随机点击
    @Override
    public Response dynamic(HttpServletRequest request, HttpServletResponse response) {
        Response response1=new Response();
        try {
            // 设置响应的类型格式为图片格式
            response.setContentType("image/jpeg");
            //禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

            HttpSession session = request.getSession();

            VerificationCodeImgUtil vCode = new VerificationCodeImgUtil(120,40,5,100);
            session.setAttribute("code", vCode.getCode());
            vCode.write(response.getOutputStream());
            response1.put("vCode",vCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response1;

    }

}
